package com.huadao.cart_ccs.greendao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.huadao.cart_ccs.greendao.entity.AddViruses;
import com.huadao.cart_ccs.greendao.entity.EnvironmentParams;
import com.huadao.cart_ccs.greendao.gen.AddCellDao;
import com.huadao.cart_ccs.greendao.gen.AddVirusesDao;
import com.huadao.cart_ccs.greendao.gen.CO2Dao;
import com.huadao.cart_ccs.greendao.gen.DaoMaster;
import com.huadao.cart_ccs.greendao.gen.DaoSession;
import com.huadao.cart_ccs.greendao.gen.DeviceRecordDao;
import com.huadao.cart_ccs.greendao.gen.EnvironmentParamsDao;
import com.huadao.cart_ccs.greendao.gen.ExceptionInfoDao;
import com.huadao.cart_ccs.greendao.gen.ExperimentStageDao;
import com.huadao.cart_ccs.greendao.gen.InjectionDao;
import com.huadao.cart_ccs.greendao.gen.LiquidLoopPlanDao;
import com.huadao.cart_ccs.greendao.gen.LogInfoDao;
import com.huadao.cart_ccs.greendao.gen.O2Dao;
import com.huadao.cart_ccs.greendao.gen.OperationDao;
import com.huadao.cart_ccs.greendao.gen.ProjectDao;
import com.huadao.cart_ccs.greendao.gen.ProtocolDao;
import com.huadao.cart_ccs.greendao.gen.SampleDao;
import com.huadao.cart_ccs.greendao.gen.TemperatureDao;

import org.greenrobot.greendao.AbstractDaoMaster;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseOpenHelper;
import org.greenrobot.greendao.database.StandardDatabase;
import org.greenrobot.greendao.identityscope.IdentityScopeType;

public class MyDaoMaster extends AbstractDaoMaster {
    public static final int SCHEMA_VERSION = 1;

    /** Creates underlying database table using DAOs. */
    public static void createAllTables(Database db, boolean ifNotExists) {
        LogInfoDao.createTable(db, ifNotExists);
        ProjectDao.createTable(db, ifNotExists);
        CO2Dao.createTable(db, ifNotExists);
        O2Dao.createTable(db, ifNotExists);
        TemperatureDao.createTable(db, ifNotExists);
        InjectionDao.createTable(db, ifNotExists);
        SampleDao.createTable(db, ifNotExists);
        ProtocolDao.createTable(db, ifNotExists);
        ExperimentStageDao.createTable(db,ifNotExists);
        ExceptionInfoDao.createTable(db,ifNotExists);
        OperationDao.createTable(db,ifNotExists);
        EnvironmentParamsDao.createTable(db,ifNotExists);
        DeviceRecordDao.createTable(db,ifNotExists);
        LiquidLoopPlanDao.createTable(db,ifNotExists);
        AddCellDao.createTable(db,ifNotExists);
        AddVirusesDao.createTable(db,ifNotExists);
    }

    /** Drops underlying database table using DAOs. */
    public static void dropAllTables(Database db, boolean ifExists) {
        LogInfoDao.dropTable(db, ifExists);
        ProjectDao.dropTable(db, ifExists);
        CO2Dao.dropTable(db, ifExists);
        O2Dao.dropTable(db, ifExists);
        TemperatureDao.dropTable(db, ifExists);
        InjectionDao.dropTable(db, ifExists);
        SampleDao.dropTable(db, ifExists);
        ProtocolDao.dropTable(db, ifExists);
        ExperimentStageDao.dropTable(db, ifExists);
        ExceptionInfoDao.dropTable(db, ifExists);
        OperationDao.dropTable(db, ifExists);
        EnvironmentParamsDao.dropTable(db, ifExists);
        DeviceRecordDao.dropTable(db, ifExists);
        LiquidLoopPlanDao.dropTable(db, ifExists);
        AddCellDao.dropTable(db, ifExists);
        AddVirusesDao.dropTable(db, ifExists);
    }

    /**
     * WARNING: Drops all table on Upgrade! Use only during development.
     * Convenience method using a {@link DaoMaster.DevOpenHelper}.
     */
    public static DaoSession newDevSession(Context context, String name) {
        Database db = new DaoMaster.DevOpenHelper(context, name).getWritableDb();
        DaoMaster daoMaster = new DaoMaster(db);
        return daoMaster.newSession();
    }

    public MyDaoMaster(SQLiteDatabase db) {
        this(new StandardDatabase(db));
    }

    public MyDaoMaster(Database db) {
        super(db, SCHEMA_VERSION);
        registerDaoClass(LogInfoDao.class);
        registerDaoClass(ProjectDao.class);
        registerDaoClass(CO2Dao.class);
        registerDaoClass(O2Dao.class);
        registerDaoClass(TemperatureDao.class);
        registerDaoClass(InjectionDao.class);
        registerDaoClass(SampleDao.class);
        registerDaoClass(ProtocolDao.class);
        registerDaoClass(ExperimentStageDao.class);
        registerDaoClass(ExceptionInfoDao.class);
        registerDaoClass(OperationDao.class);
        registerDaoClass(EnvironmentParamsDao.class);
        registerDaoClass(DeviceRecordDao.class);
        registerDaoClass(LiquidLoopPlanDao.class);
        registerDaoClass(AddCellDao.class);
        registerDaoClass(AddVirusesDao.class);
    }

    public DaoSession newSession() {
        return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
    }

    public DaoSession newSession(IdentityScopeType type) {
        return new DaoSession(db, type, daoConfigMap);
    }

    /**
     * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} -
     */
    public static abstract class OpenHelper extends DatabaseOpenHelper {
        public OpenHelper(Context context, String name) {
            super(context, name, SCHEMA_VERSION);
        }

        public OpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
            super(context, name, factory, SCHEMA_VERSION);
        }

        @Override
        public void onCreate(Database db) {
            Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION);
            createAllTables(db, false);
        }
    }

    /** WARNING: Drops all table on Upgrade! Use only during development. */
    public static class DevOpenHelper extends DaoMaster.OpenHelper {
        public DevOpenHelper(Context context, String name) {
            super(context, name);
        }

        public DevOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
            super(context, name, factory);
        }

        @Override
        public void onUpgrade(Database db, int oldVersion, int newVersion) {
            Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
//            dropAllTables(db, true);
//            onCreate(db);

            MigrationHelper.getInstance().migrate(db,LogInfoDao.class,ProjectDao.class,CO2Dao.class,O2Dao.class,TemperatureDao.class,InjectionDao.class,SampleDao.class,ProtocolDao.class,ExperimentStageDao.class,ExceptionInfoDao.class,OperationDao.class,EnvironmentParamsDao.class,DeviceRecordDao.class,LiquidLoopPlanDao.class,AddCellDao.class,AddVirusesDao.class);
        }
    }

}
